package pxene.phantomJs;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import pxene.phantomJs.util.SingleDriver;

public class PhantomJs4QQ_news {
	public static void main(String[] args) throws Exception {
//		System.setProperty("phantomjs.binary.path", "C:/Users/chenjinghui/Desktop/phantomjs-2.1.1-windows/bin/phantomjs.exe");
//		ArrayList<String> cliArgsCap = new ArrayList<String>();
//		cliArgsCap.add("--proxy=localhost:8080");
//		cliArgsCap.add("--proxy-auth=username:password");
//		cliArgsCap.add("--proxy-type=http");
//		cliArgsCap.add("--load-images=false"); //不加载图片
//		DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
//		capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
		
//		WebDriver driver = new PhantomJSDriver(capabilities);

		// 页面初始化加载超时设置
		// driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

//		driver.get("http://coral.qq.com/1797558899");

		WebDriver driver = SingleDriver.getInstance();
		
		// 控制滚动条一直向下翻页
		JavascriptExecutor js = (JavascriptExecutor) driver;
		while (true) {
			int size0 = driver.getPageSource().length();
			js.executeScript("var q=document.documentElement.scrollTop=100000");
			Thread.sleep(1000);
			int size2 = driver.getPageSource().length();
			if (size2 == size0) {
				System.out.println("翻到页底。。");
				break;
			}
		}
		
		driver.switchTo().frame("commentIframe");
		//翻页
		while (true) {
//			boolean hasMore = doesWebElementExist(driver, By.xpath("//div[@id='loadMore']/span[text()='加载更多']")); //通过内容匹配
//			boolean hasMore = doesWebElementExist(driver, By.xpath("//div[@id='loadMore']/span[text()='加载更多' and not(@style)]")); //通过属性匹配
			boolean hasMore = doesWebElementExist(driver, By.xpath("//div[@id='loadMore']/span")); //通过属性匹配
			
			boolean displayed = driver.findElement(By.xpath("//div[@id='loadMore']/span")).isDisplayed(); //确保元素没有被隐藏
			if (hasMore && displayed==true) {
				driver.findElement(By.xpath("//div[@id='loadMore']/span")).click();
				System.out.println("-----------");
			} else {
				break;
			}
			Thread.sleep((long) (Math.random() * 2000 + 2000));
		}
		
		List<WebElement> comments = driver.findElements(By.xpath("//div[@class='np-post-body']"));
		for (WebElement webElement : comments) {
			String content = webElement.findElement(By.xpath("./div[@class='np-post-content']/p")).getText();
			String userid = webElement.findElement(By.xpath(".//a[@class='np-user popClick ']")).getAttribute("post_uid");
			String userName= webElement.findElement(By.xpath(".//a[@class='np-user popClick ']")).getAttribute("title");
			String data= webElement.findElement(By.xpath(".//span[@class='np-time']")).getAttribute("data");
			String zNum= webElement.findElement(By.xpath(".//a[@class='np-btn np-btn-upvote upvote']/em")).getText();
			System.out.println("content:::"+content);
			System.out.println("userid:::"+userid);
			System.out.println("userName:::"+userName);
			System.out.println("data:::"+data);
			System.out.println("zNum::::"+zNum);
			System.out.println("=========================================================");
		}

		System.out.println(".....over");
	}
	
	/**
	 * 判断元素是否存在
	 * 
	 * @param driver
	 * @param selector
	 * @return
	 */
	public static boolean doesWebElementExist(WebDriver driver, By selector) {
		try {
			driver.findElement(selector);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}
